package com.ldl.origami.origamiService;

import com.ldl.origami.dao.TutorialMapper;
import com.ldl.origami.domain.Tutorial;
import com.ldl.origami.domain.TutorialPage;
import com.ldl.origami.util.OrigamiUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能描述:
 * 作者: LDL
 * 创建时间: 2014/7/19 20:50
 */
@Service
public class TutorialService {

    static org.slf4j.Logger logger = LoggerFactory.getLogger(TutorialService.class);
    @Autowired(required = false)
    private TutorialMapper tutorialMapper;


    /**
     * 保存教程
     * @param tutorial
     * @throws Exception
     */
    public void saveTutorial(Tutorial tutorial) throws Exception{
        try{
            tutorialMapper.insert(tutorial);
        }catch (Exception e){
            logger.debug("保存教程失败:" + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
    }

    /**
     * 更新教程
     *
     * @param tutorial
     * @throws Exception
     */
    public void updateTutorial(Tutorial tutorial) throws Exception {
        try {
            tutorialMapper.updateByPrimaryKeySelective(tutorial);
        } catch (Exception e) {
            logger.debug("更新教程失败:" + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
    }

    /**
     * 根据id查询教程
     * @param id
     * @return tutorial
     * @throws Exception
     */
    public Tutorial getTutorialById(int id) throws Exception {

        Tutorial tutorial = null;
        try {
            tutorial = tutorialMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            logger.debug("根据ID查询教程失败:" + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
        return tutorial;
    }

    /**
     * 更新教程评分次数和总分数
     * @param tutorial
     */
    public void updateScore(Tutorial tutorial) throws Exception {
        try {
            tutorialMapper.updateByPrimaryKeySelective(tutorial);
        } catch (Exception e) {
            logger.debug("更新教程评分次数和总分数失败: "+OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
    }


    /**
     * 更新教程浏览量
     * @param tutorial
     */
    public void updateViews(Tutorial tutorial) throws Exception {
        try {
            tutorialMapper.updateByPrimaryKeySelective(tutorial);
        } catch (Exception e) {
            logger.debug("更新教程浏览量失败: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
    }


    /**
     * 获取收藏的教程
     *
     * @return
     */
    public List<Tutorial> getMyCollections(List<Integer> tutorialIds) throws Exception {

        List<Tutorial> tutorials = null;
        try {
            tutorials = tutorialMapper.selectMyCollectionByPage(tutorialIds);
        } catch (Exception e) {
            logger.debug("查询我的收藏失败: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
        return tutorials;
    }

    /**
     * 获取我的教程
     *
     * @return
     */
    public List<Tutorial> getMyTutorials(TutorialPage tutorialPage) throws Exception {

        List<Tutorial> tutorials = null;
        try {
            tutorials = tutorialMapper.selectMyTutorialsByPage(tutorialPage);
        } catch (Exception e) {
            logger.debug("查询我的教程失败: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
        return tutorials;
    }

    /**
     * 删除教程
     * @param tutorialId
     * @throws Exception
     */
    public void deleteTutorialById(int tutorialId) throws Exception {

        try {
            tutorialMapper.deleteByPrimaryKey(tutorialId);
        } catch (Exception e) {
            logger.debug("删除教程失败: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
    }
}
